package com.chinacache.maint.workorder.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.orm.hibernate.SimpleHibernateDao;
import com.chinacache.maint.workorder.entity.Field;

@Transactional
public class FieldManager {
	private SimpleHibernateDao<Field, Integer> fieldDao;

	@Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		fieldDao = new SimpleHibernateDao<Field, Integer>(sessionFactory,
				Field.class);
	}

	@Transactional(readOnly = true)
	public Field getField(Integer id) {
		return fieldDao.findById(id);
	}

	@Transactional(readOnly = true)
	public Page<Field> getAllField(Page<Field> page, String condition) {
		if (condition != null && condition.length() > 0)
			return fieldDao.findByCriteria(page, "id", true,
					new Criterion[] { Restrictions.like("name", "%"
							+ condition.trim() + "%") });
		else
			return fieldDao.findByCriteria(page, "id", true);
	}

	public void saveField(Field field) {
		fieldDao.save(field);
	}

	public void deleteField(Integer id) {
		Field field = fieldDao.findById(id);
		fieldDao.delete(field);
	}

	public void updateField(Field field) {
		fieldDao.update(field);
	}

	@Transactional(readOnly = true)
	public List<Field> getAllField() {
		return fieldDao.findAll();
	}
}
